package com.zhudi.utils;

import com.zhudi.model.entity.User;
import com.zhudi.service.UserService;
import com.zhudi.service.impl.UserServiceImpl;
import org.springframework.util.DigestUtils;

import java.util.regex.Pattern;

import static com.zhudi.contant.UserContant.SALT;

public class UserUtils {
    static UserService userService = new UserServiceImpl();

    /**
     * 获得脱敏用户
     * @param user
     * @return
     */
   public static User getSafetyUser(User user){
        User safeUser = new User();
        safeUser.setId(user.getId());
        safeUser.setUserName(user.getUserName());
        safeUser.setGender(user.getGender());
        safeUser.setAvatar(user.getAvatar());
        safeUser.setNickname(user.getNickname());
        safeUser.setPhone(user.getPhone());
        safeUser.setRole(user.getRole());
        safeUser.setCreateTime(user.getCreateTime());
        return safeUser;
    }

    /**
     * 获得加密面膜
     * @param password
     * @return
     */
    public static String getencryptPassword(String password){
       return  DigestUtils.md5DigestAsHex((SALT + password).getBytes());
    }

    /**
     * 检查手机号是否合法
     * @param phoneNumber
     * @return
     */
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
            return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return false;
    }


}
